package com.swz.dcrm.ui.mine;


import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.swz.dcrm.adpter.AddressBookAdapter;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.model.PersonalInfo;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import in.myinnos.alphabetsindexfastscrollrecycler.IndexFastScrollRecyclerView;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.MainViewModel;
import com.swz.dcrm.util.Tool;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration;
import com.xh.baselibrary.callback.OnClickListener;

import java.util.Collections;
import java.util.Comparator;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class AddressBookFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.rv)
    IndexFastScrollRecyclerView rv;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;

    private AddressBookAdapter addressBookAdapter;

    @Inject
    MainViewModel mainViewModel;

    public static AddressBookFragment newInstance() {

        return new AddressBookFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())){
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }
        mainViewModel.getAddressBook().observe(this,pageBaseResponse -> {
            if(pageBaseResponse.isSuccess()){

                if (rv.getItemDecorationCount() > 0) {
                    for (int i = 0; i < rv.getItemDecorationCount(); i++) {
                        rv.removeItemDecoration(rv.getItemDecorationAt(i));
                    }
                }
                addressBookAdapter=new AddressBookAdapter(getContext(),pageBaseResponse.getData(),0);
                addressBookAdapter.setOnClickListener(onClickListener);
                Collections.sort(pageBaseResponse.getData(), new Comparator<PersonalInfo>() {
                    @Override
                    public int compare(PersonalInfo personalInfo, PersonalInfo personalInfo1) {
                        String p = Tool.ToPinyin(personalInfo.getName());
                        String p1=Tool.ToPinyin(personalInfo1.getName());


                        String o1=p.substring(0,1).toUpperCase();
                        String o2=p1.substring(0,1).toUpperCase();

                        return o1.compareTo(o2);
                    }
                });
                StickyRecyclerHeadersDecoration stickyRecyclerHeadersDecoration=new StickyRecyclerHeadersDecoration(addressBookAdapter);
                rv.addItemDecoration(stickyRecyclerHeadersDecoration);
                rv.setAdapter(addressBookAdapter);
            }
        });
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }

    @Override
    public int layoutId() {
        return R.layout.fragment_address_book;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        smartRefreshLayout.setEnableLoadmore(false);
        smartRefreshLayout.setEnableRefresh(false);
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        rv.setIndexBarTransparentValue(0);
        rv.setIndexBarTextColor(R.color.blue_0f6eff);
        rv.setIndexBarStrokeVisibility(false);
        title.setText(getString(R.string.address_book));
        return true;
    }


    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }

    OnClickListener<PersonalInfo> onClickListener=  personalInfo-> {
        if(TextUtils.isEmpty(personalInfo.getPhone())){
            return;
        }
        mainViewModel.getCall().setValue(personalInfo.getPhone());
    };
}
